<template>
  <div class="container">
    <div class="login">
      <div class="item">
        <h2>Bing酒店管理系统</h2>
      </div>
      <div class="item">
        <span>账号：</span>
        <div>
          <el-input v-model="loginId" placeholder="请输入账号" size="mini" />
        </div>
      </div>
      <div class="item">
        <span>密码：</span>
        <div>
          <el-input v-model="loginPwd" type="password" placeholder="请输入密码" size="mini" />
        </div>
      </div>
      <div class="item">
        <span>记住我：</span>
        <div>
          <el-checkbox v-model="ckMe" size="large" />
        </div>
      </div>
      <div class="item">
        <span></span>
        <el-button size="mini" type="primary" @click="adminLogin">登录</el-button>
        <el-button size="mini">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {reactive, toRefs, onBeforeMount} from 'vue';
import {useRouter} from 'vue-router';
import {login, loginAuto} from '../api/admin';

export default {
  name: 'Login',
  setup(){
    const $router = useRouter();

    let loginData = reactive({
      loginId: '', //账号
      loginPwd: '', //密码
      ckMe: false, //记住我
    });

    onBeforeMount(async () => {
      if(await loginAuto()){
        //自动登录成功
        $router.push('/layout');
      }
    });

    //点击登录按钮
    const adminLogin = async () => {
      const isSuccess = await login(loginData);

      if(isSuccess){
        //登录成功
        $router.push('/layout');
      }
    }

    return {
      ...toRefs(loginData),
      adminLogin,
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, rgb(18,55,80), lightblue);
  display: flex;
  justify-content: center;
  align-items: center;
  .login {
    width: 400px;
    border: solid 1px #EEE;
    border-radius: 6px;
    color: #EEE;
    font-family: 'bing';
    padding: 10px;
    .item {
      display: flex;
      align-items: center;
      margin: 10px 0;
      h2 {
        text-align: center;
        flex: 1;
      }
      span {
        width: 60px;
        font-size: 14px;
        text-align: right;
      }
      div {
        flex: 1;
      }
    }
  }
}
</style>